﻿/**********************
DataStorage Module
**********************/
var DataStorage = (function()
{
    var self =
    {
        OpenDatabase: function()
        {
            return "Open Database";
        },
        InsertItem: function()
        {
            return "Insert Item";
        }
    }
    return self;
})();


/**********************
ViewManger Module
***********************/
var ViewManager = (function()
{
    var views = {};
    var activeView = null;


    function getViewActions(view)
    {
        var html = "";
        for (var ii = 0; view && (ii < view.actions.length); ii++) {
            html += "<li>" + view.actions[ii].getAnchor() + "</li>";
        }

        return html;
    }

    function getAction(view, actionId)
    {
        // extract the id portion from the action id
        actionId = actionId.replace(/^action_(\d+)$/i, "$1");

        // find the specified view in the active view and execute it
        for (var ii = 0; ii < view.actions.length; ii++) {
            if (view.actions[ii].id == actionId) {
                return view.actions[ii];
            }
        }
        return null;
    }

    function switchView(oldView, newView)
    {
        var ii, menu = $("#menu").get(0);
        
        // switch the views using jQuery
        oldView ? $("#" + oldView.id).hide() : null;
        newView ? $("#" + newView.id).show().trigger("activated") : null;

        // if we have a menu, then update the actions
        if (menu) {
            // clear the menu and create list items and anchors as required
            jQuery(menu).html(getViewActions(activeView));

            // attach a click handler to each of the anchors now in the menu
            $(menu).find("a").click(function(evt)
            {
                var action = getAction(activeView, this.id);
                if (action) {
                    action.execute();
                    evt.preventDefault();
                }
            });
        }
    }

    var self =
    {
        hogetest: function()
        {
            return "ViewManger run out!";
        },
        activate: function(viewId)
        {
            // save the old view
            var oldView = activeView;

            // if a view id has been specified, but doesn't exist in the views, check for a div
            if (viewId && (!views[viewId]) && ($("#" + viewId).get(0))) {
                define({ id: viewId });
            }

            // update the active view
            activeView = viewId ? views[viewId] : null;

            // update the associated ui elements
            switchView(oldView, activeView);
        },
        getActiveView: function()
        {
            return activeView ? $("#" + activeView.id) : null;
        },
        define: function(args)
        {
            args = $.extend({ id: '', actions: [] }, args);

            // if the id is specified, add the view to the list of defined views
            if (args.id) {
                views[args.id] = args;
            }
        }
    }
    return self;

})();

/**********************
ViewAction Module
***********************/
var actionCounter = 0;
var ViewAction = (function(args)
{
    // private variables
    args = $.extend({label: "", run: null}, args);

    var self =
    {
        hogetest: function()
        {
            return "ViewAction run out!";
        },
        id: actionCounter++,
        
        getAnchor: function()
        {
            return "<a href='#' id='action_" + self.id + "'>" + args.label + "</a>";
        },
        execute: function()
        {
            if (args.run) 
            {
                args.run.apply(this, arguments);
            }
        }
    }
    return self;
})();

/**********************
ChangeViewAction Module
***********************/
var ChangeViewAction = (function(args)
{
    // if the target is not defined, then raise an error
    if (!args.target) 
    {
        throw new Error("Unable to create a ChangeViewAction without a target specified.");
    }

    // prep the label to equal the target if not defined
//    if (!args.label) 
//    {
//        args.label = args.target;
//    }
//    
//    return new ViewAction($.extend({
//        run: function()
//        {
//            ViewManager.activate(args.target);
//        }
//    }, args));
})();

alert("aaaaaaa");